package com.sparkseries.common.security.domain;

import com.sparkseries.common.util.exception.BusinessException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Objects;

/**
 * 当前操作用户
 */
public class CurrentUser {
    /**
     * 获取当前操作用户信息
     */
    public static UserDetails getInfo() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

            if (Objects.nonNull(authentication) && authentication.isAuthenticated()
                    && authentication.getPrincipal() instanceof SecurityUser) {
                return (SecurityUser) authentication.getPrincipal();
            }
        } catch (Exception e) {
            throw new BusinessException("e");
        }

        return new SecurityUser();
    }
}
